python处理csv文件列错位,Python:比较两个csv文件中的特定列

您所在的位置:网站首页 Python csv 合并 错位 python处理csv文件列错位,Python:比较两个csv文件中的特定列

python处理csv文件列错位,Python:比较两个csv文件中的特定列

2024-07-07 04:14| 来源: 网络整理| 查看: 265

Say that I have two CSV files (file1 and file2) with contents as shown below:

file1:

fred,43,Male,"23,45",blue,"1, bedrock avenue"

file2:

fred,39,Male,"23,45",blue,"1, bedrock avenue"

I would like to compare these two CSV records to see if columns 0,2,3,4, and 5 are the same. I don't care about column 1.

What's the most pythonic way of doing this?

EDIT:

Some example code would be appreciated.

EDIT2:

Please note the embedded commas need to be handled correctly.

解决方案

I suppose the best ways is to use Python library: http://docs.python.org/library/csv.html.

UPDATE (example added):

import csv

reader1 = csv.reader(open('data1.csv', 'rb'), delimiter=',', quotechar='"'))

row1 = reader1.next()

reader2 = csv.reader(open('data2.csv', 'rb'), delimiter=',', quotechar='"'))

row2 = reader2.next()

if (row1[0] == row2[0]) and (row1[2:] == row2[2:]):

print "eq"

else:

print "different"



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3